package com.example.demo.service;

import com.example.demo.enums.BookStatus;
import com.example.demo.mapper.BookInfoMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.model.RequestPage;
import com.example.demo.model.ResultPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Slf4j
@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public String addBook(BookInfo bookInfo) {
        try {
            log.info("bookService 添加图书 bookInfo:{}", bookInfo);
            Integer result = bookInfoMapper.addBook(bookInfo);
            if (result == 1) {
                return "";
            } else {
                log.info("bookService 层添加图书失败");
                return "添加图书失败";
            }
        } catch (Exception e) {
            log.error("bookService添加图书返回异常{}", e);
            return "bookService添加图书返回异常";
        }
    }

    /**
     * 获取图书总数。通过offset 和 limit 获取页面数量
     *
     * @param requestPage
     * @return
     */
    public ResultPage<BookInfo> getListByPage(RequestPage requestPage) {
        try {
            log.info("向数据库发起requestPage：{}", requestPage);
            Integer total = bookInfoMapper.getCount();
            List<BookInfo> books = bookInfoMapper.selectByRequest(requestPage);
            for (BookInfo book : books
            ) {
                book.setStatusCN(BookStatus.getStatus(book.getStatus()).getDsc());
            }
            ResultPage<BookInfo> resultPage = new ResultPage<>(total, books, requestPage);
            return resultPage;
        } catch (Exception e) {
            log.error("数据库返回失败e{}", e);
            return null;
        }

    }


    public BookInfo queryBookById(Integer id) {
        log.info("id{}",id);
        try{
            BookInfo bookInfo= bookInfoMapper.queryBookById(id);
            log.info("数据库传回来的bookInfo{}",bookInfo);
            return bookInfo;
        }catch (Exception e){
            log.error("mapper层发生异常{}",e);
            return null;
        }

    }


    public  Integer updateBook(BookInfo bookInfo) {
        log.info("bookInfo:{}",bookInfo);
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteById(Integer id) {
        try{
            log.info("id:{}",id);
            BookInfo bookInfo=new BookInfo();
            bookInfo.setId(id);
            bookInfo.setStatus(0);
            return bookInfoMapper.updateBook(bookInfo);
        }catch (Exception e){
            log.error("e:{}",e);
            return null;
        }
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        try{
            log.info("ids:{}",ids);
            Integer result=bookInfoMapper.batchDeleteBook(ids);
            return result;
        }catch (Exception e){
            log.error("e:{}",e);
            return null;
        }
    }
}

